package com.ruoyi.system.controller;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.UserDeclaration;
import com.ruoyi.system.service.IUserDeclarationService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报单Controller
 *
 * @author triangle
 * @date 2022-07-28
 */
@Controller
@RequestMapping("/system/userDeclaration")
public class UserDeclarationController extends BaseController {
    private String prefix = "system/userDeclaration";

    @Autowired
    private IUserDeclarationService userDeclarationService;

    @RequiresPermissions("system:userDeclaration:view")
    @GetMapping()
    public String userDeclaration() {
        return prefix + "/userDeclaration";
    }

    /**
     * 查询报单列表
     */
    @RequiresPermissions("system:userDeclaration:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserDeclaration userDeclaration) {
        startPage();
        SysUser sysUser= (SysUser) SecurityUtils.getSubject().getPrincipal();
        userDeclaration.setCreateBy(sysUser.getUserName());
        List<UserDeclaration> list = userDeclarationService.selectUserDeclarationList(userDeclaration);
        return getDataTable(list);
    }

    /**
     * 导出报单列表
     */
    @RequiresPermissions("system:userDeclaration:export")
    @Log(title = "报单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserDeclaration userDeclaration) {
        SysUser sysUser= (SysUser) SecurityUtils.getSubject().getPrincipal();
        userDeclaration.setDecPersonName(sysUser.getUserName());
        List<UserDeclaration> list = userDeclarationService.selectUserDeclarationList(userDeclaration);
        ExcelUtil<UserDeclaration> util = new ExcelUtil<UserDeclaration>(UserDeclaration.class);
        return util.exportExcel(list, "报单数据");
    }

    /**
     * 新增报单
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {

        SysUser user = getSysUser();
        mmap.put("user", user);
        return prefix + "/add";
    }

    /**
     * 新增保存报单
     */
    @RequiresPermissions("system:userDeclaration:add")
    @Log(title = "报单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserDeclaration userDeclaration) throws Exception {
        return toAjax(userDeclarationService.insertUserDeclaration(userDeclaration));
    }

    /**
     * 修改报单
     */
    @RequiresPermissions("system:userDeclaration:edit")
    @GetMapping("/edit/{decId}")
    public String edit(@PathVariable("decId") Long decId, ModelMap mmap) {
        UserDeclaration decDO = userDeclarationService.selectUserDeclarationByDecId(decId);
        if (StringUtils.equals(decDO.getDecStatus(), "已核销")
                ||StringUtils.equals(decDO.getDecStatus(), "已作废")) {
            throw new ServiceException("不允许编辑销" + decDO.getDecStatus() + "状态的报单!");
        }
        mmap.put("userDeclaration", decDO);
        return prefix + "/edit";
    }

    /**
     * 修改保存报单
     */
    @RequiresPermissions("system:userDeclaration:edit")
    @Log(title = "报单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UserDeclaration userDeclaration) {
        UserDeclaration decDO = userDeclarationService.selectUserDeclarationByDecId(userDeclaration.getDecId());
        if (StringUtils.equals(decDO.getDecStatus(), "已核销")
                ||StringUtils.equals(decDO.getDecStatus(), "已作废")) {
            throw new ServiceException("不允许编辑销" + decDO.getDecStatus() + "状态的报单!");
        }
        return toAjax(userDeclarationService.updateUserDeclaration(userDeclaration));
    }

    /**
     * 删除报单
     */
    @RequiresPermissions("system:userDeclaration:remove")
    @Log(title = "报单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(userDeclarationService.deleteUserDeclarationByDecIds(ids));
    }

    /**
     * 报单数据管理作废
     */
    @RequiresPermissions("system:userDeclaration:cancel")
    @Log(title = " 报单数据管理作废", businessType = BusinessType.UPDATE)
    @PostMapping("/cancel")
    @ResponseBody
    public AjaxResult cancel(String ids) {
        String[] strings = Convert.toStrArray(ids);
        Arrays.stream(strings)
                .filter(StringUtils::isNotBlank)
                .mapToLong(Long::parseLong)
                .forEach(id -> {
//                    Declaration decDO = declarationService.selectDeclarationByDecId(id);
//                    if(!StringUtils.equals(decDO.getDecStatus(),"已报单")){
//                        throw new ServiceException("不允许作废"+decDO.getDecStatus()+"状态的报单!");
//                    }

                    UserDeclaration declaration = new UserDeclaration();
                    declaration.setDecId(id);
                    declaration.setDecStatus("已作废");
                    userDeclarationService.updateUserDeclaration(declaration);
                });

        return toAjax(strings.length);
    }

}
